package test

import config.MyFile
import org.apache.commons.io.FileUtils

object GetSeqs extends MyFile {

  def main(args: Array[String]): Unit = {

    //Hordeum_vulgare_goldenpromise
     getSeqs("D:\\上海农科院大麦基因组\\Hordeum_vulgare_goldenpromise","Hordeum_vulgare_goldenpromise.GPv1.cds.all.fa","Hordeum_vulgare_goldenpromise.cds")
     getSeqs("D:\\上海农科院大麦基因组\\Hordeum_vulgare_goldenpromise","Hordeum_vulgare_goldenpromise.GPv1.pep.all.fa","Hordeum_vulgare_goldenpromise.pep")

    //Hordeum_vulgare_tritex
    // getSeqs("D:\\上海农科院大麦基因组\\Hordeum_vulgare_tritex","Hordeum_vulgare_tritex.Morex_V2_scaf.cds.all.fa","Hordeum_vulgare_tritex.cds")
    // getSeqs("D:\\上海农科院大麦基因组\\Hordeum_vulgare_tritex","Hordeum_vulgare_tritex.Morex_V2_scaf.pep.all.fa","Hordeum_vulgare_tritex.pep")

  }

  def getSeqs(path: String, input: String, output: String) = {
    val row = s"$path/$input".readFileToString.split(">").tail.map { x =>
      val line = x.split("\n")
      val gene = line.head.split(" ").find(_.startsWith("gene:")).get.drop(5)
      gene -> line.tail.mkString
    }.groupBy(_._1).map { x => ">" + x._1 + "\n" + x._2.maxBy(_._2.length)._2 }.mkString("\n")
    FileUtils.writeStringToFile(s"$path/$output".toFile, row)
  }
}
